/////////////////////////////////////////////////////////////////////////////
/*/	
 *	Creator:		Joshua C. Rouzer
 *	Title:			Level Select State
 *	Last Edited:	07/15/2013
 *	Summary:		
/*/
/////////////////////////////////////////////////////////////////////////////
#include "SinglePlayerState.h"
#include "LevelSelectState.h"
#include "MainMenuState.h"
#include "LevelManager.h"
#include "OptionsMenuState.h"
#include "CSGD_TextureManager.h"
#include "CSGD_DirectInput.h"
#include "BitmapFont.h"
#include "../resource.h"
#include <sstream>
/////////////////////////////////////////////////////////////////////////////
// Function		: Default Constructor
// Notes		: Starts at the first available level
/////////////////////////////////////////////////////////////////////////////
CLevelSelectState::CLevelSelectState(){
	m_nCurrSelect = 0;
	CLevelManager::GetInstance()->SetLevelID(0);
	for(int iter = 0; iter < LEVEL_COUNT; iter++){
		examples[iter] = -1;
	}
}
/////////////////////////////////////////////////////////////////////////////
// Function		: Destructor
// Notes		: Cleans up any dynamically allocated memory
/////////////////////////////////////////////////////////////////////////////
CLevelSelectState::~CLevelSelectState(){}
/////////////////////////////////////////////////////////////////////////////
// Function		: Get Instance
// Return		: An instance of the level select state
// Notes		: 
/////////////////////////////////////////////////////////////////////////////
CLevelSelectState* CLevelSelectState::GetInstance(){
	static CLevelSelectState s_Instance;
	return &s_Instance;
}
/////////////////////////////////////////////////////////////////////////////
// Function		: Enter
// Notes		: 
/////////////////////////////////////////////////////////////////////////////
void CLevelSelectState::Enter(){
	m_nCurrSelect = CLevelManager::GetInstance()->GetLevelID();
	for(int iter = 0; iter < LEVEL_COUNT; iter++){
		examples[iter] = -1;
		wostringstream filename;
		filename << "resource\\Images\\ExampleLevel" << iter + 1 << ".png";
		examples[iter] = CSGD_TextureManager::GetInstance()->LoadTexture(filename.str().c_str());
	}
	if(COptionsMenuState::GetInstance()->ReturnEng())
	{
		LoadString(CGame::GetInstance()->ReturnInsance(), ID_Eng_LevelSelect, LevelSelectName, 40);
		LoadString(CGame::GetInstance()->ReturnInsance(), ID_Eng_PrsEnter, PrsEnter, 40);
	}
	else
	{
		LoadString(CGame::GetInstance()->ReturnInsance(), ID_Swa_LevelSelect, LevelSelectName, 40);
		LoadString(CGame::GetInstance()->ReturnInsance(), ID_Swa_PrsEnter, PrsEnter, 40);
	}
	
	lvlsltName = "";
	prsEnterS = "";
	for (int character = 0; character < 40; character++)
	{
		if(LevelSelectName[character] != NULL)
			lvlsltName += (char)LevelSelectName[character];
		else
			break;
	}

	for (int character = 0; character < 40; character++)
	{
		if(PrsEnter[character] != NULL)
			prsEnterS += (char)PrsEnter[character];
		else
			break;
	}
	
	
}
/////////////////////////////////////////////////////////////////////////////
// Function		: Exit
// Notes		: 
/////////////////////////////////////////////////////////////////////////////
void CLevelSelectState::Exit(){
	for(int iter = 0; iter < LEVEL_COUNT; iter++){
		CSGD_TextureManager::GetInstance()->UnloadTexture(examples[iter]);
		examples[iter] = -1;
	}
}
/////////////////////////////////////////////////////////////////////////////
// Function		: Input
// Return		: Any input
// Notes		: 
/////////////////////////////////////////////////////////////////////////////
bool CLevelSelectState::Input(){
	//Left and right to switch between levels
	CSGD_DirectInput *pDI = CSGD_DirectInput::GetInstance();
	if(pDI->KeyPressed(DIK_LEFT) || pDI->KeyPressed(DIK_A)){
		if(m_nCurrSelect == 0){
			m_nCurrSelect = LEVEL_COUNT - 1;
		}
		else{
			m_nCurrSelect--;
		}
	}
	if(pDI->KeyPressed(DIK_RIGHT) || pDI->KeyPressed(DIK_D)){		
		if(m_nCurrSelect == LEVEL_COUNT -1){
			m_nCurrSelect = 0;
		}
		else{
			m_nCurrSelect++;
		}
	}
	if(pDI->KeyPressed(DIK_RETURN)){
		CLevelManager::GetInstance()->SetCurrLevel(m_nCurrSelect);
		CGame::GetInstance()->ChangeState(CSinglePlayerState::GetInstance());
	}
	if(pDI->KeyPressed(DIK_ESCAPE)){
		CGame::GetInstance()->ChangeState(CMainMenuState::GetInstance());
	}
	return true;
}
/////////////////////////////////////////////////////////////////////////////
// Function		: Update
//Parameters	: fElapsedTime - The ammount of time that has passed since last update
// Notes		: 
/////////////////////////////////////////////////////////////////////////////
void CLevelSelectState::Update(float fElapsedTime){}
/////////////////////////////////////////////////////////////////////////////
// Function		: Render
// Notes		: 
/////////////////////////////////////////////////////////////////////////////
void CLevelSelectState::Render(){
	//Show the level
	RECT rect;
	SetRect(&rect, 0, 0, 800, 600);
	CSGD_TextureManager::GetInstance()->Draw(examples[m_nCurrSelect], 0, 0, 1.0f, 1.0f, &rect);

	CBitmapFont* font = CGame::GetInstance()->GetFont();

	font->Draw( LevelSelectName, CGame::GetInstance()->GetScreenMidPoint(lvlsltName,1.0f), 50, 1.0f, 
		D3DCOLOR_ARGB( 255, 255, 0, 0 ) );
	font->Draw( _T("<"), 100, 50, 1.0f, 
		D3DCOLOR_ARGB( 255, 255, 0, 0 ) );
	font->Draw( _T(">"), 700, 50, 1.0f, 
		D3DCOLOR_ARGB( 255, 255, 0, 0 ) );
	font->Draw( PrsEnter, CGame::GetInstance()->GetScreenMidPoint(prsEnterS,1.0f), 150, 1.0f, 
		D3DCOLOR_ARGB( 255, 255, 0, 0 ) );
}